//
//  RMMTaoCanDetailsViewController.m
//  Romomo
//
//  Created by puman on 2016/12/28.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMTaoCanDetailsViewController.h"
#import "RMMGoodsDetailsHeadView.h"
#import "RMMTaoCanDetialBottomView.h"
#import "RMMTaoCanDetialsTableViewCell.h"
#import "RMMGoodsDetialsViewController.h"
#import "RMMTaoCanConfirmViewController.h"
#import "RMMLongingViewController.h"
#import "RMMHomeShareView.h"

@interface RMMTaoCanDetailsViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation RMMTaoCanDetailsViewController
{
    RMMGoodsDetailsHeadView *_headView;
    RMMTaoCanDetialBottomView *_bottomView;
    RMMNotNetWorkView *_networkView;
    NSArray *_dataArray;
    UITableView *_tableView;
    NSDictionary *_dic;
    CGFloat _rowHeight;
    CGFloat _rowNetHeight;
    NSString *_xiangMuJieShao;
    NSString *_noticeString;
    NSMutableArray *_stringArray;
    RMMHomeShareView *_shareView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"套餐详情";
    self.view.backgroundColor = kWhiteColor;
    [self addSubViews];
    [self getRequestData];
    [self addRightItem];
    // Do any additional setup after loading the view.
}
- (void)addRightItem{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share11@3x"] style:(UIBarButtonItemStylePlain) target:self action:@selector(share)];
    rightItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = rightItem;
}
- (void)getRequestData{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"org_no"] = Org_no;
    parDic[@"bom_id"] = _bom_id;
    NSString *url = TaoCanDetilsUrl;
    [OSRequestManager requestWithUrlString:url parDic:parDic network:^(BOOL isNetwork) {
        if (!isNetwork) {
            _networkView.hidden = NO;
        } else {
            _networkView.hidden = YES;
        }
    } method:GET finish:^(NSDictionary *dataDic) {
        _stringArray = [NSMutableArray array];
        NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
        if ([status isEqualToString:@"1"]) {
            _headView.taoCanDic = [RMMFileManager setValueForDic:dataDic[@"data"][@"PROD_BOM"]];
            _dic = [RMMFileManager setValueForDic:dataDic[@"data"]];
            _bottomView.totalPriceLabel.text = [NSString stringWithFormat:@"总计: ¥%@",[RMMFileManager setValueForDic:_dic[@"PROD_BOM"]][@"PRICE"]];
            _dataArray = _dic[@"PROD_SERVICES"];
            _noticeString = [RMMFileManager setValueForDic:_dic[@"PROD_BOM"]][@"SERCER_NOTICE"];
            _xiangMuJieShao = [RMMFileManager setValueForDic:_dic[@"PROD_BOM"]][@"IMAGE_DETAIL_DESCRIBE"];
            _rowHeight = [UILabel backHeightWithString:_xiangMuJieShao CGFloatCount:kScreenWideth - 30 metho:HEIGHT font:14];
            _rowNetHeight = [UILabel backHeightWithString:_noticeString CGFloatCount:kScreenWideth - 30 metho:HEIGHT font:14];

        } else {
            [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
        }
        for (NSDictionary *dic in _dataArray) {
            NSString *string = [NSString stringWithFormat:@"%@:%@次",dic[@"SERVICE_NAME"],dic[@"SERVICE_QTY"]];
            [_stringArray addObject:string];
        }
        [_tableView reloadData];
    } error:^(NSError *errors) {
        
    } view:self.view];    
}

- (void)addSubViews{
    _noticeString = @"";
    _xiangMuJieShao = @"";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64 - 49) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    [_tableView registerClass:[RMMTaoCanDetialsTableViewCell class] forCellReuseIdentifier:@"TaoCanCell"];
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 0)];
    [self addTableViewHeadView];
    
    _bottomView = [[RMMTaoCanDetialBottomView alloc] initWithFrame:CGRectMake(0, _tableView.bottom, kScreenWideth, 49)];
    BUTTON_S(_bottomView.goBuyBut, buttonClick);
    if ([_mySkill isEqualToString:@"1"]) {
        _bottomView.hidden = YES;
    } else {
        _bottomView.hidden = NO;
    }
    [self.view addSubview:_bottomView];
    
    _shareView = [[RMMHomeShareView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight)];
    _shareView.hidden = YES;
    UIWindow *widow = [UIApplication sharedApplication].keyWindow;
    [widow addSubview:_shareView];
    // 分享按钮
    for (int i = 0; i < 5; i++) {
        UIButton *button = [_shareView viewWithTag:50 + i];
        BUTTON_S(button, shareButtonClick:);
    }

    _networkView = [[RMMNotNetWorkView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenHeight - 64)];
    _networkView.hidden = YES;
    BUTTON_S(_networkView.button, networkBut);
    [self.view addSubview:_networkView];
}
- (void)networkBut{
    [self getRequestData];
}
- (void)addTableViewHeadView{
    _headView = [[RMMGoodsDetailsHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, kScreenWideth + 2 * kHalf + 1)];
    _tableView.tableHeaderView = _headView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return _dataArray.count;
    } else {
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 90;
    } else if (indexPath.section == 1){
        return _rowHeight + 20;
    } else {
        return _rowNetHeight + 20;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 44)];
        view.backgroundColor = COLORFROMCODE(0xF5F6F7, 1);
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.width - 30, view.height)];
        label.font = kFont(13);
        label.numberOfLines = 0;
        label.textColor = COLORFROMCODE(0x9B9B9B, 1);
        label.text = [NSString stringWithFormat:@"可享受%@",[_stringArray componentsJoinedByString:@","]];
        [view addSubview:label];
        return view;
    } else  {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWideth, 44)];
        view.backgroundColor = COLORFROMCODE(0xF5F6F7, 1);
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenWideth - 30, view.height)];
        label.font = kFont(13);
        label.textColor = COLORFROMCODE(0x9B9B9B, 1);
        if (section == 1) {
            label.text = @"项目介绍";
        } else {
            label.text = @"注意事项";
        }
        [view addSubview:label];
        return view;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 49;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        RMMTaoCanDetialsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TaoCanCell"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.dic = _dataArray[indexPath.row];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.font = kFont(14);
        cell.textLabel.textColor = COLORFROMCODE(0x2C2C2C , 1);
        cell.textLabel.numberOfLines = 0;
        if (indexPath.section == 1) {
            cell.textLabel.text = _xiangMuJieShao;
        } else {
            cell.textLabel.text = _noticeString;
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        RMMGoodsDetialsViewController *VC = [[RMMGoodsDetialsViewController alloc] init];
        VC.service_id = _dataArray[indexPath.row][@"SERVICE_ID"];
        [self.navigationController pushViewController:VC animated:YES];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
}
- (void)buttonClick{
    [self creatBomOrder];
}
- (void)creatBomOrder{
    if ([RMMFileManager shareHandle].isLoginSuccess) {
        NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
        NSString *cust_id = [RMMFileManager retrunObjWith:[RMMFileManager shareHandle].userDic[@"CUST_ID"]];
        parDic[@"org_no"] = Org_no;
        parDic[@"cust_id"] = cust_id;
        parDic[@"bom_name"] = _dic[@"PROD_BOM"][@"NAME"];
        parDic[@"bom_id"] = _dic[@"PROD_BOM"][@"BOM_ID"];
        parDic[@"payment"] = _dic[@"PROD_BOM"][@"PRICE"];
        parDic[@"order_source"] = @"1";
        parDic[@"region_id"] = [RMMFileManager shareHandle].region_id;
        NSString *url = CreaterBomOrderUrl;
        [OSRequestManager requestWithUrlString:url parDic:parDic method:POST finish:^(NSDictionary *dataDic) {
            NSString *status = [NSString stringWithFormat:@"%@",dataDic[@"status"]];
            if ([status isEqualToString:@"1"]) {
                RMMTaoCanConfirmViewController *VC = [[RMMTaoCanConfirmViewController alloc] init];
                VC.dic = _dic;
                VC.orderDic = dataDic[@"data"];
                [self.navigationController pushViewController:VC animated:YES];
            } else {
                [RMMFileManager MBProgressWith:dataDic[@"message"] onView:self.view];
            }
        } error:^(NSError *errors) {
            
        } view:self.view];
    } else {
        [self addLoginAlertView];
    }
}
- (void)addLoginAlertView{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加购物车,需要您先前往登录" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *actionA = [UIAlertAction actionWithTitle:@"前往" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        RMMLongingViewController *loginVC = [[RMMLongingViewController alloc] init];
        [self.navigationController pushViewController:loginVC animated:YES];
        
    }];
    UIAlertAction *actionB = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertController addAction:actionA];
    [alertController addAction:actionB];
    [self presentViewController:alertController animated:YES completion:nil];
}
// 分享
- (void)share{
    _shareView.hidden = NO;
    [UIView animateWithDuration:kInterval animations:^{
        _shareView.view.frame = CGRectMake(0, kScreenHeight - 230, kScreenWideth, 230);
        _shareView.backView.alpha = 1;
    } completion:^(BOOL finished) {
    }];
}
- (void)shareButtonClick:(UIButton *)button{
    [UIView animateWithDuration:kInterval animations:^{
        _shareView.view.frame = CGRectMake(0, kScreenHeight, kScreenWideth, 230);
        _shareView.backView.alpha = 0.0;
    } completion:^(BOOL finished) {
        _shareView.hidden = YES;
    }];
    NSArray *array = @[@(UMSocialPlatformType_WechatSession),@(UMSocialPlatformType_WechatTimeLine),@(UMSocialPlatformType_QQ),@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_Qzone)];
    
    UMSocialPlatformType platformType = - 8;
    platformType = [array[button.tag - 50] integerValue];
    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
    if (button.tag == 53) {
        // UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
        // shareObject.thumbImage = [UIImage imageNamed:@"Icon-iPhone-60@2x"];
        messageObject.text = [NSString stringWithFormat:@"http://ydywx.puman.cn/Web/Goods_Content.aspx?SerId=1917&Flg=1"];
        // messageObject.shareObject = shareObject;
    } else {
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"容么么" descr:@"容么么周末大减价买一送10" thumImage:[UIImage imageNamed:@"Icon-iPhone-60@3x"]];
        //设置网页地址
        shareObject.webpageUrl =@"http://ydywx.puman.cn/Web/Goods_Content.aspx?SerId=1917&Flg=1";
        messageObject.shareObject = shareObject;
    }
    [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id result, NSError *error) {
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
